package com.cjie.ecommerce.controller;

import com.cjie.ecommerce.entity.Category;
import com.cjie.ecommerce.entity.Product;
import com.cjie.ecommerce.service.ProductWithSimpleCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 商品控制器
 */
@RestController
@RequestMapping("/simple-cache/products")
@Slf4j
public class ProductWithSimpleCacheController {

    private final ProductWithSimpleCacheService productWithSimpleCacheService;

    @Autowired
    public ProductWithSimpleCacheController(ProductWithSimpleCacheService productWithSimpleCacheService) {
        this.productWithSimpleCacheService = productWithSimpleCacheService;
    }

    /**
     * 获取产品信息，带多级缓存。
     * 示例URL：<a href="http://localhost:8080/simple-cache/products/product-multi-cache-spel/10001">...</a>
     *
     * @param id 产品编号
     */
    @GetMapping("/product-multi-cache-spel/{id}")
    public Product getProductWithMultiCache(@PathVariable Long id) {
        Product product = productWithSimpleCacheService.getProductByIdWithMultiCache(id);
        log.debug("getProductByIdWithMultiCache: {}", product);
        return product;
    }

    /**
     * 获取产品信息，带多级缓存，缓存键使用OGNL表达式生成。
     * 示例URL：<a href="http://localhost:8080/simple-cache/products/product-multi-cache-ognl/10001">...</a>
     *
     * @param id 产品编号
     */
    @GetMapping("/product-multi-cache-ognl/{id}")
    public Product getProductWithMultiCacheOgnl(@PathVariable Long id) {
        Product product = productWithSimpleCacheService.getProductByIdWithMultiCacheOgnl(id);
        log.debug("getProductByIdWithMultiCacheOgnl: {}", product);
        return product;
    }

    /**
     * 获取产品信息，带本地缓存。
     * 示例URL：<a href="http://localhost:8080/simple-cache/products/product-local-cache/10001">...</a>
     *
     * @param id 产品编号
     */
    @GetMapping("/product-local-cache/{id}")
    public Product getProductWithLocalCache(@PathVariable Long id) {
        Product product = productWithSimpleCacheService.getProductByIdWithLocalCache(id);
        log.debug("getProductByIdWithLocalCache: {}", product);
        return product;
    }

    /**
     * 获取指定分类下的精选产品，带本地缓存。
     * 示例URL：http://localhost:8080/simple-cache/products/selected-products-by-category-local-cache
     * 示例请求参数：
     * {
     *     "id": "a0001",
     *     "name": ""
     * }
     *
     * @param category 类别对象【仅用于演示相关缓存注解的用法，实际项目中是不会这么传数据的】
     * @return
     */
    @GetMapping("/selected-products-by-category-local-cache")
    private List<Product> getSelectedProductsByCategory(@RequestBody Category category) {
        List<Product> productList = productWithSimpleCacheService.getSelectedProductsByCategory(category);
        log.debug("getSelectedProductsByCategory: {}", productList);
        return productList;
    }

    /**
     * 获取热门产品
     * <p>
     * 示例URL：http://localhost:8080/simple-cache/products/hot-products
     */
    @GetMapping("/hot-products")
    public List<Product> getHotProducts() {
        List<Product> productList = productWithSimpleCacheService.getHotProducts();
        log.debug("getHotProducts: {}", productList);
        return productList;
    }

    /**
     * 获取所有产品列表
     * <p>
     * 示例URL：http://localhost:8080/simple-cache/products/all-products
     */
    @GetMapping("/all-products")
    public List<Product> getAllProducts() {
        List<Product> productList = productWithSimpleCacheService.getAllProducts();
        log.debug("getAllProducts: {}", productList);
        return productList;
    }

    /**
     * 更新产品信息
     *
     * @param product 新产品信息
     */
    @PutMapping("/update-product")
    public Product updateProduct(@RequestBody Product product) {
        Product updatedProduct = productWithSimpleCacheService.updateProduct(product);
        log.debug("updateProduct: {}", updatedProduct);
        return updatedProduct;
    }

    /**
     * 删除产品
     *
     * @param id 产品id
     */
    @DeleteMapping("/delete-product/{id}")
    public void deleteProduct(@PathVariable Long id) {
        productWithSimpleCacheService.deleteProduct(id);
    }

    /**
     * 创建产品信息
     *
     * @param product 新产品信息
     */
    @PostMapping("/create-product")
    public Product createProduct(@RequestBody Product product) {
        return productWithSimpleCacheService.createProduct(product);
    }

    /**
     * 获取所有的分类信息
     */
    @GetMapping("/all-categories")
    public List<Category> getAllCategories() {
        return productWithSimpleCacheService.getAllCategories();
    }

    /**
     * 获取产品的详细信息，使用指定的方法生成缓存key
     * 示例URL：<a href="http://localhost:8080/simple-cache/products/product-detail/1">...</a>
     */
    @GetMapping("/product-detail/{id}")
    public Map<String, Object> getProductDetail(@PathVariable Long id) {
        return productWithSimpleCacheService.getProductDetail(id);
    }
}